import { RandomFunMethod, RandomFunType } from './randomFunType'

class RandomFun {
  private randomFunList: RandomFunType[] = [
    {
      id: '1',
      name: '1-10随机数',
      funtionCode: 'return Math.floor(Math.random() * 10) + 1;'
    }
  ]

  public addRandomFun(randomFun: RandomFunType) {
    this.randomFunList.push(randomFun)
  }

  public getRandomFunList() {
    return this.randomFunList
  }

  public removeRandomFun(randomFun: RandomFunType) {
    this.randomFunList = this.randomFunList.filter((item) => item.id !== randomFun.id)
  }

  public clearRandomFun() {
    this.randomFunList = []
  }

  public getRandomFunById(id: string) {
    return this.randomFunList.find((item) => item.id === id)
  }

  public executeMethod<M extends RandomFunMethod>(
    methodName: M,
    ...args: Parameters<this[M]>
  ): ReturnType<this[M]> {
    const method = this[methodName] as Function
    const result = method.apply(this, args)
    return result
  }
}

export default new RandomFun()
